Android RecyclerView 实现快速滑动

##前言:
使用RecyclerView时,调用smoothScrollToPostion()方法滑动到指定位置,但是条目很多时滑动的很慢,本篇文章就是实现RecyclerView的快速滑动。

先介绍如何实现,然后再介绍原理。

##1. 实现代码

  1. 创建FastScrollLinearLayoutManager,继承LinearLayoutManager
  2. 复写smoothScrollToPosition()方法,主要复写LinearSmoothScroller中方法

代码如下,解释全在注释中:

public class FastScrollLinearLayoutManager extends LinearLayoutManager {
    public FastScrollLinearLayoutManager(Context context) {
        super(context);
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
	   LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

			@Override 
	        public PointF computeScrollVectorForPosition(int targetPosition) {
				return	FastScrollLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
	        }

	        //该方法控制速度。
	        //if returned value is 2 ms, it means scrolling 1000 pixels with LinearInterpolation should take 2 seconds.
	        @Override 
	        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
		        /*
		             控制单位速度,  毫秒/像素, 滑动1像素需要多少毫秒.
		
		             默认为 (25F/densityDpi) 毫秒/像素
		
		             mdpi上, 1英寸有160个像素点, 25/160,
		             xxhdpi,1英寸有480个像素点, 25/480,
		          */

	              //return 10F / displayMetrics.densityDpi;//可以减少时间,默认25F
	             return super.calculateSpeedPerPixel(displayMetrics);
	        }

		   //该方法计算滑动所需时间。在此处间接控制速度。
	       //Calculates the time it should take to scroll the given distance (in pixels)
	       @Override 
	       protected int calculateTimeForScrolling(int dx) {
		       /*
		           控制距离, 然后根据上面那个方(calculateSpeedPerPixel())提供的速度算出时间,
		           
		           默认一次 滚动 TARGET_SEEK_SCROLL_DISTANCE_PX = 10000个像素,
		
		           在此处可以减少该值来达到减少滚动时间的目的.
		        */

				//间接计算时提高速度,也可以直接在calculateSpeedPerPixel提高
	            if (dx > 3000) {
	                dx = 3000;
	            }

	            int time = super.calculateTimeForScrolling(dx);
	            LogUtil.d(time);//打印时间看下
	
	            return time;
			}
		};

	    linearSmoothScroller.setTargetPosition(position);
	    startSmoothScroll(linearSmoothScroller);
    }
}

从复写的两个方法可以看出,都是为了提高滑动速度。一种是直接修改速度,另外一种是通过减少距离来减少所需时间,间接提高滑动速度。

这两种方法都可以,看自己所需。接下来就讲讲实现的原理。这块我只是梳理的大致的流程,不过至此你已经可以实现快速滑动了。

##2. RecyclerView 滑动过程梳理
1.调用RecyclerView.smoothScrollToPosition(position)时

public void smoothScrollToPosition(int position) {
		//...直接调用了LayoutManager的该方法
        mLayout.smoothScrollToPosition(this, mState, position);
    }

2.LinearLayoutManager中

@Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
            int position) {
        LinearSmoothScroller linearSmoothScroller = new  LinearSmoothScroller(recyclerView.getContext()) {
                  //...
                };
		//设置终点位置
        linearSmoothScroller.setTargetPosition(position);
        //开始滚动,使用LinearSmoothScroller(一直匀速滑动,当targetPosition出现在屏幕上时再减速滑动),startSmoothScroll()是LayoutManager中的方法
        startSmoothScroll(linearSmoothScroller);
    }

3.LayoutManager中

public void startSmoothScroll(SmoothScroller smoothScroller) {
     //...
     mSmoothScroller = smoothScroller;
     //调用SmoothScroller.start()方法开始滚动,this参数指当前LayoutManager
     mSmoothScroller.start(mRecyclerView, this);
}

4.SmoothScroller中

void start(RecyclerView recyclerView, LayoutManager layoutManager) {
	//...
	//使用ViewFlinger进行动画,ViewFlinger实现了Runnable接口,并且内部使用了Scroller,这样就可以post自己进而对RecyclerView不断layout就可以实现滑动
    mRecyclerView.mViewFlinger.postOnAnimation();
}

5.ViewFlinger中,这是实现滑动的重点,省略了很多代码逻辑

private class ViewFlinger implements Runnable {
	 @Override
     public void run() {
		if (scroller.computeScrollOffset()) {
			//调用SmoothScroller的onAnimation方法
			smoothScroller.onAnimation(dx - overscrollX, dy - overscrollY);
		}
	 }
}

6.SmoothScroller中

private void onAnimation(int dx, int dy) {
	//...
    if (mTargetView != null) {
	    // verify target position
        if (getChildPosition(mTargetView) == mTargetPosition) {
		    //要滑动到的位置已经显示在屏幕上,onTargetFound()方法里update了差值器,由线性差值器变成了减速的差值器。
	        onTargetFound(mTargetView, recyclerView.mState, mRecyclingAction);
             mRecyclingAction.runIfNecessary(recyclerView);
         }
         
         //...

        if (mRunning) {
	        //再下一次滑动
	        onSeekTargetStep(dx, dy, recyclerView.mState, mRecyclingAction);
	        //调用内部类Action的runIfNecessary方法
            mRecyclingAction.runIfNecessary(recyclerView);
            }
        }

7.Action中

private void runIfNecessary(RecyclerView recyclerView) {
	//调用了ViewFlinger.smoothScrollBy()方法,并传入了mDuration,mDuration是在SmoothScroller中upDate()时传入的,就是由前文讲的两个方法共同决定的
	recyclerView.mViewFlinger.smoothScrollBy(mDx, mDy, mDuration, mInterpolator);
}

8.ViewFlinger中开始滚动

public void smoothScrollBy(int dx, int dy, int duration, Interpolator interpolator) {
            if (mInterpolator != interpolator) {
                mInterpolator = interpolator;
                mScroller = ScrollerCompat.create(getContext(), interpolator);
            }
            setScrollState(SCROLL_STATE_SETTLING);
            mLastFlingX = mLastFlingY = 0;
            //调用Scroller开始滚动,此处即duration
            mScroller.startScroll(0, 0, dx, dy, duration);
            postOnAnimation();
        }

这块粗略的按照流程说了一下滚动过程,涉及的类比较多,最终通过Scroller来进行滚动。

##结语:
本篇文章实现了RecyclerView的快速滚动,但需要注意一个问题:如果你的Item比较复杂,滚动起来会卡顿。 这个在看源码时的一个注释里面有提到,后来实践时确实也发现。不得不说微信朋友圈滑动起来的真的快,它用的是ListView,貌似开启了FastEnable属性。
同时也可以仿照知乎,先使用RecyclerView.scrollToPosition(position)直接滑动到某一个位置后再使用smoothScrollToPosition(0)滑动到顶部。这块在RecyclerView里的Action类中jumpTo()的注释里有提到,如果很远的话可以先到一个位置后再滑动。

这两种滑动到顶部的方式都实现了一个小Demo。测试代码在GitHub上 FastScrollFragment
另外在自己写的小项目上也用上了 ZhihuDaily,可以查看这两个Demo来具体了解。

  • 12
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baiiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值